﻿using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Tippelde.Services.Bets;
using Tippelde.Services.Dtos;
using Tippelde.Services.Groups;
using Tippelde.Web.Helpers;
using Tippelde.Web.Models.Bet;

namespace Tippelde.Web.Controllers
{
    [Authorize]
    public class BetController : Controller
    {
        [Dependency]
        public IBetManager BetManager { get; set; }
        [Dependency]
        public IGroupManager GroupManager { get; set; }

        public ActionResult Index(int? Id)
        {
            if (Id.HasValue)
            {
                try
                {
                    var model = new CompetitionTableModel{
                        Table = BetManager.GetCompetitionTable(User.Identity.Name, Id.Value),
                        Groups = CacheManager.Current.GetGroupsForUser()
                    };
                    return View(model);
                }
                catch (Exception ex)
                {
                    var model = new CompetitionTableModel
                    {
                        Groups = CacheManager.Current.GetGroupsForUser()
                    };
                    ModelState.AddModelError("", ex);
                    return View(model);
                }
            }
            else
            {
                var model = new CompetitionTableModel
                {
                    Groups = CacheManager.Current.GetGroupsForUser()
                };
                return View(model);
            }
        }

        public ActionResult Bet(int eventId)
        {

            return PartialView();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Bet(EventWithBetData model)
        {
            if(ModelState.IsValid)
            {
                try
                {
                    BetManager.AddBet(User.Identity.Name, model.Id, model.Bet.HomeBet, model.Bet.AwayBet);
                    model.HasBet = true;
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex);
                }
            }
            TempData["ModelState"] = ModelState;
            //return PartialView("_BetForEvent", model);
            return RedirectToAction("Row", "Event", new { Id = model.Id });
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(EventWithBetData model)
        {
            try
            {
                BetManager.DeleteBet(User.Identity.Name, model.Id);
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex);
            }
            TempData["ModelState"] = ModelState;
            //return PartialView("_BetForEvent", model);
            return RedirectToAction("Row", "Event", new { Id = model.Id });
        }
	}
}